package cn.sc.summer.gateway.util;

import cn.sc.summer.constant.util.BaseIpUtil;
import cn.sc.summer.gateway.exception.CommonException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;

/**
 * 类名：针对请求获取ip地址值，并进行转换hash
 *
 * @author a-xin
 * @date 2023/7/27 16:10
 */
@Slf4j
@Component
public class IPUtil extends BaseIpUtil {

    /**
     * 获取网关request的ip地址
     *
     * @param request 请求体
     * @return ip地址
     */
    public static String getIpAddr(ServerHttpRequest request) {
        HttpHeaders headers = request.getHeaders();
        String ip = headers.getFirst("x-forwarded-for");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值，第一个ip才是真实ip
            if (ip.contains(",")) {
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddress().getAddress().getHostAddress();
        }
        return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
    }

    /**
     * 获取请求体的真是IP地址(若被代理，则获取真实IP)
     *
     * @param request 请求体
     * @return ip地址
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ipAddress = request.getHeader("x-forwarded-for");
        String unknown = "unknown";
        final String IPV_4 = "127.0.0.1";
        final String IPV_6 = "0:0:0:0:0:0:0:1";
        if (ipAddress == null || ipAddress.length() == 0 || unknown.equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || unknown.equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || unknown.equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if (IPV_4.equals(ipAddress) || IPV_6.equals(ipAddress)) {
                ///根据网卡取本机配置的IP
                try {
                    InetAddress inet = InetAddress.getLocalHost();
                    if (inet != null) {
                        ipAddress = inet.getHostAddress();
                    }
                } catch (Exception e) {
                    throw new CommonException("IP address acquisition exception: ", e);
                }
            }
        }
        if (IPV_4.equals(ipAddress) || IPV_6.equals(ipAddress)) {
            ///根据网卡取本机配置的IP
            try {
                InetAddress inet = InetAddress.getLocalHost();
                if (inet != null) {
                    ipAddress = inet.getHostAddress();
                }
            } catch (Exception e) {
                throw new CommonException("IP address acquisition exception: ", e);
            }
        }
        //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        String split = ",";
        if (ipAddress != null && ipAddress.length() > 15) {
            if (ipAddress.indexOf(split) > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(split));
            }
        }
        return ipAddress;
    }

}
